package com.mdc.mansion.web.modules.pay.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mdc.mansion.web.modules.system.utils.UserUtils;
import com.mdc.mansion.dataaccess.modules.system.entity.User;

import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.pay.entity.RestaurantPayCfg;
import com.mdc.mansion.core.modules.pay.RestaurantPayCfgService;
import com.mdc.mansion.web.common.web.BaseController;
/**
 * 在线支付配置Controller
 * @author xizf
 * @version 2018-08-20
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/restaurantPayCfg")
public class RestaurantPayCfgController extends BaseController {

	@Autowired
	private RestaurantPayCfgService restaurantPayCfgService;
	
	@ModelAttribute
	public RestaurantPayCfg get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return restaurantPayCfgService.get(id);
		}else{
			return new RestaurantPayCfg();
		}
	}
	
	@RequiresPermissions("pay:restaurantPayCfg:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(RestaurantPayCfg restaurantPayCfg) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<RestaurantPayCfg> page = restaurantPayCfgService.findPage(restaurantPayCfg);
        model.put("page", page);
		ModelAndView modelAndView=new ModelAndView("modules/pay/restaurantPayCfgList", model);
		return modelAndView;
	}
	
	@RequiresPermissions("pay:restaurantPayCfg:view")
	@RequestMapping(value = "form")
	public ModelAndView form(RestaurantPayCfg restaurantPayCfg) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("restaurantPayCfg", restaurantPayCfg);
		return new ModelAndView("modules/pay/restaurantPayCfgForm",model);
	}
	

	@RequiresPermissions("pay:restaurantPayCfg:edit")
	@RequestMapping(value = "save")
	public ModelAndView save(RestaurantPayCfg restaurantPayCfg,RedirectAttributes redirectAttributes) {
		User u=UserUtils.getUser();
		restaurantPayCfg.setUpdateBy(u);
		if (StringUtils.isNotBlank(restaurantPayCfg.getId())){
			restaurantPayCfgService.updateSelective(restaurantPayCfg);
		}else{
			restaurantPayCfg.setCreateBy(u);
			restaurantPayCfgService.save(restaurantPayCfg);
		}
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("pay:restaurantPayCfg:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(RestaurantPayCfg restaurantPayCfg,RedirectAttributes redirectAttributes) {
		restaurantPayCfgService.delete(restaurantPayCfg);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
